<template>
	<view class="resultTask">
		<uni-nav-bar
			color="#ffffff"
			background-color="#00aded"
			:statusBar="true"
			title="计次作业"
			fixed="true"
			left-icon="left"
			left-text="返回"
			@clickLeft="back"
			right-icon="plus"
			@clickRight="addCount"
		></uni-nav-bar>
		<view class="resultTask_top">
			<uni-section title="点击图形开始扫描" type="line" titleFontSize="20px">
				<uni-icons type="scan" size="80" @click="onScan"></uni-icons>
			</uni-section>
		</view>
		<view class="resultTask_top">
			<view class="" style="margin-top: 10rpx; margin-bottom: 10rpx"></view>
			<uni-section title="运单一览" type="line" title-font-size="20px">
				<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0" class="dilSearch"></dil-search>
				<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
			</uni-section>
		</view>
		<view class="editActualWeight">
			<uni-popup ref="editActualWeight_popup" border-radius="10px 10px 0 0" background-color="#fff" style="width: 100%; height: 100%">
				<view class="editActualWeight_main" style="margin: 10px">
					<dil-Form formId="777" ref="form" v-model="formValue" class="dilForm" labelPosition="right" labelWidth="140px"></dil-Form>
				</view>
				<view class="editActualWeight_floot" style="text-align: center">
					<button type="warn" size="mini" style="margin: 10px" @click="closePopup()">取消</button>
					<button type="primary" size="mini" style="margin: 10px" @click="editActualWeightSubmit()">提交</button>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			formValue: {},
			transOrderNumber: null,
			options: {
				requestUrl: '/api/v1/tms/getTransOrderList?apiId=627',
				requestQuery: {},
				cardSlotList: [
					// {
					// 	label: '加一趟',
					// 	icon: 'plus',
					// 	emitEvent: 'addCount'
					// },
					// {
					// 	label: '减一趟',
					// 	icon: 'minus',
					// 	emitEvent: 'delCount'
					// },
					{
						label: '填写次数',
						icon: 'plus',
						emitEvent: 'updCount'
					},
					{
						label: '关闭',
						icon: 'closeempty',
						emitEvent: 'close'
					}
				],
				columnDataList: [],
				totalNum: 0,
				rowDataList: []
			}
		};
	},
	onShow() {
		this.getOrderSection();
	},
	watch: {},
	mounted() {
		this.getOrderSectionDebounce = this.Tool.debounce((data) => {
			this.getOrderSection(data);
		}, 500);
	},
	methods: {
		onScan() {
			let that = this;
			that.$commonScan(async (data) => {
				let scanResult = data.scanResult;
				if (!scanResult.includes('YSDD') && scanResult.length != '20') {
					uni.showToast({
						title: '识别信息不符合规则,请重试',
						icon: 'none',
						duration: 2000
					});
				} else {
					that.transOrderNumber = scanResult;
					that.getOrderSection({
						workFlag: '计次',
						transOrderNumber: that.transOrderNumber
					});
				}
			});
		},
		onSearch(item) {
			this.getOrderSectionDebounce(item);
		},
		closePopup() {
			this.$refs.editActualWeight_popup.close();
		},
		editActualWeightSubmit() {
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '请确认填写的次数与吨位是否准确',
				success: (res) => {
					if (res.confirm) {
						that.updCountResult(that.formValue);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		back() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		async getOrderSection(data = {}) {
			data.status = '已确认';
			data.billingType = '计次';
			this.options.requestQuery = { ...data };
		},
		emitEvent(eventName, cardItem) {
			this[eventName](cardItem);
		},
		addCount(cardItem) {
			let that = this;
			cardItem.transCount = new Number(cardItem.transCount);
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '确定新增一趟？新增后趟数为：' + (cardItem.transCount + 1),
				success: (res) => {
					if (res.confirm) {
						cardItem.transCount++;
						that.updCountResult(cardItem);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		delCount(cardItem) {
			let that = this;
			cardItem.transCount = new Number(cardItem.transCount);
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '确定减少一趟？减少后趟数为：' + (cardItem.transCount - 1),
				success: (res) => {
					if (res.confirm) {
						cardItem.transCount--;
						that.updCountResult(cardItem);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		updCount(cardItem) {
			let that = this;
			cardItem.transCount = new Number(cardItem.transCount);
			this.formValue = cardItem;
			this.$refs.editActualWeight_popup.open('center');
			// uni.showModal({
			// 	cancelText: '取消',
			// 	confirmText: '确认',
			// 	title: '填写计次趟数',
			// 	content: '',
			// 	placeholderText: '请填写计次趟数',
			// 	editable: true,
			// 	success: (res) => {
			// 		if (res.confirm) {
			// 			let transCount = Number(res.content);
			// 			if (transCount != transCount) {
			// 				uni.showToast({
			// 					title: '请填写数字！',
			// 					icon: 'none',
			// 					duration: 3000
			// 				});
			// 				return;
			// 			}
			// 			cardItem.transCount = res.content;
			// 			that.updCountResult(cardItem);
			// 		} else if (res.cancel) {
			// 			uni.showToast({
			// 				title: '用户取消操作！',
			// 				icon: 'none',
			// 				duration: 3000
			// 			});
			// 		}
			// 	}
			// });
		},
		async updCountResult(data) {
			const res = await this.$myRequest({
				url: '/api/v1/tms/updCountResult',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'none'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					title: '操作成功',
					icon: 'success'
				});
				this.getOrderSectionDebounce();
				this.closePopup();
			} else {
				uni.showToast({
					duration: 2000,
					title: res.data.message,
					icon: 'none'
				});
			}
		},
		close(cardItem) {
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '确认关闭该运单？关闭后无法继续计次作业！',
				success: (res) => {
					if (res.confirm) {
						cardItem.deleted = 2;
						that.changeTransOrder(cardItem);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		async changeTransOrder(data) {
			const res = await this.$myRequest({
				url: '/api/v1/tms/changeTransOrder',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'none'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					title: '关闭成功',
					icon: 'success'
				});
				this.getOrderSectionDebounce();
			} else {
				uni.showToast({
					duration: 2000,
					title: res.data.message,
					icon: 'none'
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.resultTask {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	&_top {
		width: 100vw;
		height: 100%;
		.uni-section {
			width: 100vw;
			.dilSearch {
				width: 100%;
			}
			/deep/.uni-section-content {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				.noTip {
					width: 100vw;
					height: 10vh;
					display: flex;
					justify-content: center;
					align-items: center;
					font-weight: 700;
					font-size: 60rpx;
					color: #000;
				}
			}
		}
	}
	.resultTask_top_result {
		margin-top: 20rpx;
		.scroll-Y {
			height: 90vh;
		}
	}
	&_main {
		margin-top: 20rpx;
	}
	&_middle {
		margin-top: 20rpx;
		.uni-section {
			width: 100vw;
			/deep/.uni-section-content {
				display: flex;
				justify-content: center;
				align-items: center;
			}
			/deep/.uni-card {
				border-radius: 40rpx;
				.buttonClass {
					margin-top: 20rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}
				.button {
					border-radius: 40rpx;
				}
			}
		}
	}
	&_floot {
		width: 100vw;
		background-color: #fff;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
		border-radius: 20rpx;
	}
	.editActualWeight {
		&_main {
			.tip {
				font-size: 32rpx;
				font-weight: 500;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				text {
					margin-top: 20rpx;
				}
			}
			.importData {
				margin-top: 20rpx;
				font-size: 32rpx;
				font-weight: 700;
				color: #ff0000;
				text {
					padding: 20rpx;
				}
			}
			.dilForm {
				width: 90%;
				margin-left: 20rpx;
				/deep/.uni-forms .uni-forms-item .uni-forms-item__label {
					font-weight: 700;
					color: #000;
					font-size: 32rpx;
				}
				/deep/.uni-forms .uni-forms-item .uni-forms-item__content {
					font-weight: 700;
					color: #000;
					font-size: 32rpx;
				}

				/deep/.uni-forms .uni-forms-item .uni-forms-item__content .uni-input-placeholder {
					font-weight: 700;
					// color: #ff0000;
					font-size: 28rpx;
				}
				/deep/.uni-forms .uni-forms-item .uni-forms-item__content .uni-input-input:disabled {
					font-weight: 700;
					color: #ff0000;
					font-size: 28rpx;
				}
				// display: flex;
				// justify-content: center;
				// align-items: center;
			}
		}
	}
}
</style>
